<?php
namespace app\api\controller;

use think\Controller;
use think\Request;
use think\Db;

class Customer extends Controller
{
    /**
     * 获取客户列表
     * @param Request $request
     * @return \think\Response
     */
    public function index(Request $request)
    {
        try {
            $param = $request->param();
            if ($param) {
                $params = $param['params'] ?? [];
            }

            // 获取请求参数
            $page = $params['page']??1;
            $pageSize = $params['page_size']??10;
            $status = $params['status']??'';
            $username = $params['username']??'';
            $truename = $params['truename']??'';
            $phone = $params['phone']??'';
            $nickname = $params['nickname']??'';
            $tenant = $params['tenant']??'';


            // 构建查询条件
            $where = [];

            // 状态筛选
            if ($status !== '') {
                $where[] = ['status', '=', $status];
            }

            // 用户名称模糊查询
            if (!empty($username)) {
                $where[] = ['username', 'like', '%' . $username . '%'];
            }

            // 用户昵称模糊查询
            if (!empty($truename)) {
                $where[] = ['truename', 'like', '%' . $truename . '%'];
            }

            // 手机号模糊查询
            if (!empty($phone)) {
                $where[] = ['phone', 'like', '%' . $phone . '%'];
            }

            // 微信昵称模糊查询
            if (!empty($nickname)) {
                $where[] = ['nickname', 'like', '%' . $nickname . '%'];
            }

            $where2 = '';
            if (!empty($tenant)) {
                $tenant_user_id = explode('-',$tenant)[0];
                $c_id = Db::table("store_customer_tenant")->where(['t_id'=>$tenant_user_id])->column("c_id");
                if(count($c_id)){
                    $c_str = implode(',',$c_id);
                    $where2 = "id in ($c_str)";
                }else{
                    $where2 = "id in ('')";
                }
            }



            // 查询数据
            $query = Db::table('store_customer')
                ->where($where)->where($where2)
                ->order('id', 'desc');

            // 获取总数
            $total = $query->count();

            // 分页查询
            $list = $query->page($page, $pageSize)
                ->field([
                    'id', 'openid', 'headimg', 'nickname', 'phone',
                    'username', 'role', 'truename', 'sex',
                    'create_at', 'desc', 'status'
                ]) // 不返回密码字段
                ->select();

            // 计算总页数
            $totalPages = ceil($total / $pageSize);

            // 返回数据
            return json([
                'code' => 200,
                'message' => 'success',
                'data' => [
                    'list' => $list ?: [],
                    'total' => $total,
                    'current_page' => $page,
                    'page_size' => $pageSize,
                    'total_pages' => $totalPages
                ]
            ]);

        } catch (\Exception $e) {
            // 记录错误日志
            \think\Log::error('获取客户列表失败: ' . $e->getMessage());

            return json([
                'code' => 500,
                'message' => '服务器内部错误',
                'data' => []
            ]);
        }
    }
}